package cz.zcu.fav.rat.managed.bean.calendar;

import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;

import org.primefaces.model.ScheduleModel;

import cz.zcu.fav.rat.beans.Object;
import cz.zcu.fav.rat.beans.Period;
import cz.zcu.fav.rat.beans.User;
import cz.zcu.fav.rat.managed.bean.calendar.event.ReservationEventFactory;
import cz.zcu.fav.rat.security.beans.UserSessionController;

@ManagedBean(name = "myCalendar")
@ViewScoped
public class MyCalendarBean extends AbstractCalendarBean<User>
{
	private static final long serialVersionUID = 2315507766857785962L;
	private List<Object> objects;

	@Override
	@PostConstruct
	public void init()
	{
		super.init();
		objects = getObjectservice().getAll();
	}

	@Override
	protected Integer loadIdentifier()
	{
		return UserSessionController.getLoggedUserBean().getId();
	}

	@Override
	protected ScheduleModel getModel(Integer identifier)
	{
		return new CalendarModelEventUser(identifier,
				new ReservationEventFactory());
	}

	@Override
	protected User loadItem(Integer itemId)
	{
		return getUserService().get(itemId);
	}

	public List<Object> getObjects()
	{
		return objects;
	}

	@Override
	protected ReservationEvent createNewEvent(Date since, Date to)
	{
		return new ReservationEvent("dummy", since, to, getItem(), null, null,
				null);
	}

	public void pickUpEvent(ActionEvent actionEvent)
	{
		Period period = getPeriodService().pickUp(
				getEvent().getObject().getId(), getItem());
		if (period != null)
		{
			if (!period.getReservation().getUser().equals(getItem()))
			{
				// TODO: notify somehow
			} else
			{
				// /fine
				// TODO: notify somehow
			}
		} else
		{
			// TODO: notify somehow

			// nothing was picked

		}
	}
}
